#include <errno.h>

#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <fcntl.h>



int main( int argc, char** argv)
{
    if ( argc != 1)
    {
        printf("Wrong arguments\n");
        exit(0);
    }
/////////////////////////////////////////
    mkfifo( "MAIN_FIFO", 0644);
	int fd_main_fifo = open( "MAIN_FIFO", O_RDONLY );
	
	char buf[ 1024];
	char fifo_name[9];
	
	while ( read( fd_main_fifo, fifo_name , 8) != 8);
	
	fifo_name[8] = '\0';
//////////////////////////////////////////
	int fd_fifo = open( fifo_name, O_RDONLY);
	int fd_out  = 1; //stdout
	
	int read_count;
	int write_count;
	do
	{
		read_count = read( fd_fifo, buf, 1024);
		write_count = 0;
		do
		{
			int tmp = write( fd_out, buf + write_count, read_count - write_count);
			if ( tmp == -1)
			{
				perror("Out write error");
				exit(0); 
			}
			write_count +=tmp;
		}
		while ( write_count != read_count);
		
		
	}while( read_count > 0);
	
	unlink( fifo_name);
}